package com.stedu.service.impl;

import com.stedu.bean.Book;
import com.stedu.mapper.BookMapper;
import com.stedu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Cacheable(value = "book_back", key = "'bookAll'", unless = "#result == null || #result.size() == 0")
    @Override
    public List<Book> selectAll() {
        return bookMapper.selectAll();
    }

    @CacheEvict(value = "book_back", key = "'bookAll'")
    @Override
    public boolean insertBook(Book book) {
        return bookMapper.insertBook(book) == 1;
    }

    @CacheEvict(value = "book_back", key = "'bookAll'")
    @Override
    public boolean deleteBook(Integer bid) {
        return bookMapper.deleteBook(bid) == 1;
    }

    @CacheEvict(value = "book_back", key = "'bookAll'")
    @Override
    public boolean updateBook(Book book) {
        return bookMapper.updateBook(book) == 1;
    }

    @Override
    public List<Book> selectByBname(String bname, String bauthor) {
        return bookMapper.selectByBname(bname, bauthor);
    }

    @Override
    public Book selectByBid(Integer bid) {
        return bookMapper.selectByBid(bid);
    }

    @Override
    public List<Book> selectByAuthor(String bauthor) {
        return bookMapper.selectByAuthor(bauthor);
    }

    @Override
    public List<Map<String, Object>> selectBidAndCid(String cname) {
        return bookMapper.selectBidAndCid(cname);
    }

    @Override
    public List<Book> selectBycid(Integer cid) {
        return bookMapper.selectBycid(cid);
    }

    @Override
    public Book BookWithClassifies(String bname) {
        return bookMapper.selectBookWithClassifies(bname);
    }

    @Override
    public List<Book> selectBook() {
        return bookMapper.selectBook();
    }

    @Override
    public List<Integer> selectAidByBid(Integer bid) {
        return bookMapper.selectAidByBid(bid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBidAndAid(Integer bid, Integer[] aids) {
        bookMapper.deleteBidAndAidByBid(bid);
        if (aids != null && aids.length > 0) {
            bookMapper.insertBidAndAid(bid, aids);
        }
    }
}
